/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */


/* eslint-disable */
// 网上抄的
// https://github.com/epoberezkin/fast-deep-equal

let isArray = Array.isArray;
let keyList = Object.keys;
let hasProp = Object.prototype.hasOwnProperty;

function deepCompare (a: unknown, b: unknown): boolean {
    if (a === b) {
        return true;
    }

    if (a && b && typeof a === 'object' && typeof b === 'object') {
        let arrA = isArray(a);
            let arrB = isArray(b);
            let i;
            let length;
            let key;

        if (Array.isArray(a) && Array.isArray(b)) {
            length = a.length;
            if (length !== b.length) {
                return false;
            }
            for (i = length; i-- !== 0;) {
                if (!deepCompare(a[i], b[i])) {
                    return false;
                }
            }
            return true;
        }

        if (arrA !== arrB) {
            return false;
        }

        let dateA = a instanceof Date;
            let dateB = b instanceof Date;
        if (dateA !== dateB) {
            return false;
        }
        if (a instanceof Date && b instanceof Date) {
            return a.getTime() === b.getTime();
        }

        let regexpA = a instanceof RegExp;
            let regexpB = b instanceof RegExp;
        if (regexpA !== regexpB) {
            return false;
        }
        if (regexpA && regexpB) {
            return a.toString() === b.toString();
        }

        let keys = keyList(a) as (keyof typeof a)[];
        length = keys.length;

        if (length !== keyList(b).length) {
            return false;
        }

        for (i = length; i-- !== 0;) {
            if (!hasProp.call(b, keys[i])) {
                return false;
            }
        }

        for (i = length; i-- !== 0;) {
            key = keys[i];
            if (!deepCompare(a[key], b[key])) {
                return false;
            }
        }

        return true;
    }

    return a !== a && b !== b;
}

export {
    deepCompare
};
